import { getFieldCategory, getFieldConditionItems, getCollectOptions, getFieldOperateCondition } from '@/api/filterOption';
import { getFieldList, getChartColor } from '@/api/reportManage';
const options = {
  state: {
    reportFieldList: [], // 报表字段列表
    fieldList: [], // 字段列表
    fieldCategorys: [], // 字段分类
    fieldConditionItems: [], // 字段列表
    computeOperates: [], // 运算操作符
    collectOptions: null, // 汇总选项
    colorList: [], // 统计图表颜色主题
    bgColorList: [] // 统计图表背景颜色
  },
  mutations: {
    SET_REPORTFIELDLIST: (state, data) => {
      state.reportFieldList = data;
    },
    SET_FIELDLIST: (state, data) => {
      state.fieldList = data;
    },
    SET_FIELDCATEGORYS: (state, categorys) => {
      state.fieldCategorys = categorys;
    },
    SET_FIELDCONDITIONITMES: (state, fields) => {
      state.fieldConditionItems = fields;
    },
    SET_COLLECTOPTIONS: (state, options) => {
      state.collectOptions = options;
    },
    SET_COMPUTEOPERATES: (state, operates) => {
      state.computeOperates = operates;
    },
    SET_THEME_COLORS: (state, data) => {
      state.colorList = data;
    },
    SET_BG_COLORS: (state, data) => {
      state.bgColorList = data;
    }
  },
  actions: {
    GetFieldCategorys({ commit, state }) {
      return new Promise((resolve, reject) => {
        getFieldCategory().then(res => {
          const data = res.data;
          commit('SET_FIELDCATEGORYS', data);
          resolve(data);
        }, e => {
          reject(e);
        }).catch(error => {
          reject(error);
        });
      });
    },
    GetFieldConditionItems({ commit, state }) {
      return new Promise((resolve, reject) => {
        getFieldConditionItems().then(res => {
          const data = res.data;
          commit('SET_FIELDCONDITIONITMES', data);
          resolve(data);
        }, e => {
          reject(e);
        }).catch(error => {
          reject(error);
        });
      });
    },
    GetCollectOptions({ commit, state }) {
      return new Promise((resolve, reject) => {
        getCollectOptions().then(res => {
          const data = res.data;
          commit('SET_COLLECTOPTIONS', data);
          resolve(data);
        }, e => {
          reject(e);
        }).catch(error => {
          reject(error);
        });
      });
    },
    GetComputeOperates({ commit, state }) {
      return new Promise((resolve, reject) => {
        getFieldOperateCondition().then(res => {
          const data = res.data;
          commit('SET_COMPUTEOPERATES', data);
          resolve(data);
        }, e => {
          reject(e);
        }).catch(error => {
          reject(error);
        });
      });
    },
    GetFieldList({ commit, state }) {
      return new Promise((resolve, reject) => {
        getFieldList({ name: '', use: 2 }).then(res => {
          const data = res.data;
          commit('SET_FIELDLIST', data);
          resolve(data);
        }, e => {
          reject(e);
        }).catch(error => {
          reject(error);
        });
      });
    },
    GetColorList({ commit, state }) {
      const defaultBGColor = [
        { id: 'default-1', colors: ['#46B0FF', '#4468FF'] },
        { id: 'default-2', colors: ['#F9CC22', '#FF6C39'] },
        { id: 'default-3', colors: ['#87F483', '#00CE9E'] },
        { id: 'default-4', colors: ['#9271FF', '#4646FD'] },
        { id: 'default-5', colors: ['#FFCBA5', '#F21368'] },
        { id: 'default-6', colors: ['#FF418D', '#5241FF'] },
        { id: 'default-7', colors: ['#1FC9FD', '#FC0061'] },
        { id: 'default-8', colors: ['#FF8C8C', '#00B7FF'] }
      ]; // 默认背景颜色
      return new Promise((resolve, reject) => {
        getChartColor().then(res => {
          const theme = []; // 主题色
          const background = JSON.parse(JSON.stringify(defaultBGColor)); // 背景色
          res.data.forEach(i => {
            i.isCanEdit = true;
            // type  0: 主题色  1:背景色
            if (i.type === 1) {
              background.push(i);
            } else {
              i.label = `d-${i.id}`;
              theme.push(i);
            }
          });
          commit('SET_THEME_COLORS', theme);
          commit('SET_BG_COLORS', background);
          resolve();
        }).catch(() => {
          reject();
        });
      });
    }
  }
};

export default options;

